$(document).ready(function () {
	
	loadtable();
})	

//清空顾客信息
function clearsj(){
	window.location.reload();
}

function loadtable()
{	 
    var	customerIds=$('#customerIds').val();
	
	var columns = [[
	                {field:'ids',title:'ids',hidden:true},
	                {field:'proids',title:'产品ids',hidden:true},
					{field:'proname',title:'产品名称',width:100,align:'center'},
					{field:'pronum',title:'产品剩余数量',width:100,align:'center'},
					{field:'totalexperience',title:'产品剩余使用数量',width:100,align:'center'},
					{field:'proprice',title:'购买时单价',width:100,align:'center'},
					{field:'totalprice',title:'购买时价钱',width:100,align:'center',
						formatter: function(value,row,index)
						{
							var pronum=row.pronum;
							var proprice=row.proprice;
							var totalprice=parseFloat(pronum)*parseFloat(proprice);
							return  row.totalprice=totalprice;
						}
					},
					{field:'newPrice',title:'结转金额',width:100,align:'center',
				  		editor:{type:'numberbox',
							min:0,
							options:{
								precision:2
							}
				  		}
					}
			    ]];
	$('#cuspro_datagrid').datagrid({
		
		 cls: "theme-datagrid",
		 fitColumns:true,
		singleSelect: true,
		rownumbers: true, //显示序号
		columns: columns,
		onClickRow: onClickRow,
		url:"boutShop/getCusPro?customerIds="+customerIds
	});


}

/*数据表格行编辑*/
var editIndex1 = undefined;
function endEditing(){
	if (editIndex1 == undefined){return true}
	if ($('#cuspro_datagrid').datagrid('validateRow', editIndex1)){
		$('#cuspro_datagrid').datagrid('endEdit', editIndex1);
			editIndex1 = undefined;
			return true;
	} else {
		return false;
	}
}
function onClickRow(index){
	if (editIndex1 != index){
		if (endEditing()){
			$('#cuspro_datagrid').datagrid('selectRow', index)
				.datagrid('beginEdit', index);
				editIndex1 = index;
		} else {
			$('#cuspro_datagrid').datagrid('selectRow', editIndex1);
		}
	}else{
		if($('#cuspro_datagrid').datagrid('endEdit', editIndex1)){
			$('#cuspro_datagrid').datagrid('selectRow', editIndex1)
			.datagrid('beginEdit', editIndex1);
		}
	}
}

//获取顾客信息
function searchCus() {
	var reData = getCusInfo();
	if (reData.msg == 200) {
		var customerIds = reData.customer.ids;
		
		var url="/boutShop/getCustomerInfo";
		var data={"customerIds":customerIds};
		var divid="boutShop_div";
		common_ajax.ajaxDiv(divid,url,data);
		loadtable();
	}
}
//保存转出
function  saveOut()
{	
	
	if (endEditing()){
	var customerids=$('#bsCusIds').val();
	if(customerids!="")
	{	
		//拼接顾客产品表格
		$("#allData").html("");
		var rows=$('#cuspro_datagrid').datagrid('getRows');
		if(rows.length>0)
		{
			for(var i=0;i<rows.length;i++)
			{	
				
				var proids = rows[i].proids;
				var proname = rows[i].proname;
				var pronum = rows[i].pronum;
				var totalexperience = rows[i].totalexperience;
				if(totalexperience==null||totalexperience=='undefined'){
					totalexperience = 0;
				}
				var proprice = rows[i].proprice;
				var totalprice = rows[i].totalprice;
				var newPrice = rows[i].newPrice;
				if(newPrice==null||newPrice=='undefined'||newPrice == "0.00"){
					$.alert(parent.$, "请填写结转价格");
					return false;
				}
				
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].customerIds' value='"+customerids+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].productIds' value='"+proids+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].count' value='"+pronum+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].proprice' value='"+proprice+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].totalprice' value='"+totalprice+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].totalexperience' value='"+totalexperience+"'/>");
					$("#allData").append("<input type='hidden' name='customerProductSurplusList["+i+"].jzjg' value='"+newPrice+"'/>");
					$("#allData").append("<input type='hidden' name='productSalesList["+i+"].price' value='"+newPrice+"'/>");
					$("#allData").append("<input type='hidden' name='productSalesList["+i+"].productID' value='"+proids+"'/>");
					$("#allData").append("<input type='hidden' name='productSalesList["+i+"].customerID' value='"+customerids+"'/>");
					$("#allData").append("<input type='hidden' name='productSalesList["+i+"].number' value='"+pronum+"'/>");
				
				
			
			}
		}
		
		
		var url='/boutShop/saveOutShop';
		var data=$('#boutShop_form').serialize();
		var reData = common_ajax.ajaxFunc(url,data);
		if(reData == "200")
		{	
			 $.slideMsg("转出成功", parent.$);
			 location.reload();
//			 parent.refresh();
		}else
		{
			$.alert(parent.$, reData);
		}
	}
	}
}
